<?php

namespace app\controller\admin\exam;

use app\model\Students;
use app\model\Exam;
use app\model\Questions;
use app\model\exam\Papers;
use app\service\admin\UserServiceFacade;
use laytp\controller\Backend;
use think\facade\Config;
use laytp\library\CommonFun;

/**
 * 考试记录
 */
class Record extends Backend
{

    /**
     * exam_record模型对象
     * @var \app\model\exam\Record
     */
    protected $model;
	protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public function _initialize()
    {
        $this->model = new \app\model\exam\Record();
    }

    
    //查看和搜索列表
    public function index(){
        $where  = $this->buildSearchParams();
        $order  = $this->buildOrder();

        $orgId = UserServiceFacade::getUser()->org_id;
        $studentIds = Students::where('org_id', $orgId)->column('id');
        $data   = $this->model->where($where);

        if ($orgId != 99) {
            $data = $data->whereIn('student_id', $studentIds);
        }
        $data = $data->order($order);
        $paging = $this->request->param('paging', false);
        if ($paging) {
            $limit        = $this->request->param('limit', Config::get('paginate.limit'));
            $data         = $data->paginate($limit)->toArray();
            $data['data'] = $this->getSelectedData($data['data']);
        } else {
            $data = $data->select()->toArray();
        }



        if (isset($data['data'])) {

            $questionIds = $examIds = $papersIds = $studentIds =  [];
            foreach ($data['data'] as $val)
            {
                $studentIds[] = $val['student_id'];
                $examIds[] = $val['exam_id'];
                $papersIds[] = $val['papers_id'];
                $questionIds[] = $val['question_id'];
            }
            $studentUserList = Students::whereIn('id', $studentIds)->column('name', 'id');

            $examList = Exam::whereIn('id', $examIds)
                ->column('name', 'id');
            $paperList = Papers::whereIn('id', $papersIds)
                ->column('name', 'id');
            $questionList = Questions::whereIn('id', $questionIds)
                ->column('title, anwser, difficulty', 'id');
            foreach ($data['data'] as &$row) {
                $row['answer'] = $row['answer'];
                $row['exam_name'] = $examList[$row['exam_id']] ?? '';
                $row['papers_name'] = $paperList[$row['papers_id']] ?? '';
                $row['question_name'] = $questionList[$row['question_id']]['title'] ?? '';
                $row['right_answer'] = $questionList[$row['question_id']]['anwser'] ?? '';
                $row['difficulty'] = $questionList[$row['question_id']]['difficulty'] ?? '';
                $row['student_name']  = $studentUserList[$row['student_id']] ?? '';
            }
        }
        return $this->success('数据获取成功', $data);
    }

    

    
    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        return $this->success('获取成功', $info);
    }

    

    

    
}
